<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="listQuery.name" placeholder="名称" style="width: 160px;" class="filter-item" @keyup.enter.native="handleFilter" />
      <el-date-picker v-model="listQuery.startDateStr" value-format="yyyy-MM-dd" type="date" style="width: 140px;" placeholder="开始日期" class="filter-item" />
      <el-date-picker v-model="listQuery.endDateStr" value-format="yyyy-MM-dd" type="date" style="width: 140px;" placeholder="结束日期" class="filter-item" />
<!--      <el-select v-model="listQuery.status" placeholder="状态" clearable style="width: 90px" class="filter-item">-->
<!--        <el-option v-for="(value, key) in statusMap" :key="key" :label="value" :value="key" />-->
<!--      </el-select>-->

      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        搜索
      </el-button>

    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column label="序号" prop="id" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1}}</span>
        </template>
      </el-table-column>

      <el-table-column label="用户ID" prop="id" align="center" width="80">
        <template slot-scope="scope">
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column label="昵称" align="center" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.nickName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="头像" align="center" min-width="150px">
        <template slot-scope="scope">
          <img :src="scope.row.avatarUrl" width="100px" height="100px">
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" min-width="150px">
        <template slot-scope="{row}">
          <span>{{ row.sex | sexFilter }}</span>
        </template>
      </el-table-column>
      <el-table-column label="注册时间" width="150px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后登陆时间" width="150px" align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.lastLoginTime | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="作者" width="110px" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ scope.row.author }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="浏览量" width="110px" align="center">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ scope.row.viewCount }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="状态" class-name="status-col" width="100">-->
<!--        <template slot-scope="{row}">-->
<!--          <el-tag :type="row.status | statusStyle">-->
<!--            {{ row.status | statusFilter }}-->
<!--          </el-tag>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" width="230" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <router-link :to="'/member/edit/'+scope.row.id">
            <el-button type="primary" size="small" icon="el-icon-edit">
              编辑
            </el-button>
          </router-link>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page.sync="listQuery.curPageNum" :limit.sync="listQuery.pageSize" @pagination="getList" />

  </div>
</template>

<script>
import { fetchList } from '@/api/member'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'Member',
  components: { Pagination },
  directives: { waves },
  filters: {
    statusStyle(status) {
      const statusStyleMap = {
        '1': 'success',
        '2': 'info'
        // 3: 'danger'
      }
      return statusStyleMap[status]
    },
    statusFilter(status) {
      const statusMap = {
        '1': '上线中',
        '2': '已下线'
      }
      return statusMap[status]
    },
    sexFilter(sex) {
      const sexMap = {
        '1': '男',
        '2': '女'
      }
      return sexMap[sex]
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        name: undefined,
        curPageNum: 1,
        pageSize: 10,
        status: undefined,
        title: undefined,
        author: undefined,
        startDateStr: undefined,
        endDateStr: undefined
      },
      statusMap: { '1': '上线中', '2': '已下线' },
      temp: {
        id: undefined,
        content: '',
        createDate: '',
        updateDate: '',
        title: '',
        status: ''
      },
      dialogStatus: '',
      textMap: {
        update: '编辑',
        create: '新增'
      },
      rules: {
        type: [{ required: true, message: 'type is required', trigger: 'change' }],
        timestamp: [{ type: 'date', required: true, message: 'timestamp is required', trigger: 'change' }],
        title: [{ required: true, message: 'title is required', trigger: 'blur' }]
      },
      downloadLoading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      fetchList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleDelete(row) {
      this.$notify({
        title: 'Success',
        message: 'Delete Successfully',
        type: 'success',
        duration: 2000
      })
      const index = this.list.indexOf(row)
      this.list.splice(index, 1)
    }
  }
}
</script>
